//
//  NearbyDetailScrollView.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/22.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "NearbyDetailScrollView.h"

@implementation NearbyDetailScrollView
-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        [self createSubViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
    
}

-(void)createSubViews:(NearbyDetail *)nearbyDetail {
    self.nearbyDetail = nearbyDetail;
    UIImageView *imgaeV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 200)];
    [imgaeV sd_setImageWithURL:[NSURL URLWithString:nearbyDetail.photo] placeholderImage:[UIImage imageNamed:@"icon1024.png"]];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imgaeV addGestureRecognizer:tap];
    imgaeV.userInteractionEnabled = YES;
    [self addSubview:imgaeV];
    UIImageView *photoImgae = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH - 40, 200 - 40, 30, 30)];
    photoImgae.image = [UIImage imageNamed:@"photo.png"];
    [imgaeV addSubview:photoImgae];
    
    
    BaseLabel *nameL = [[BaseLabel alloc] initWithFrame:CGRectMake(0, imgaeV.frame.size.height + 10, WIDTH, 30)];
    nameL.text = nearbyDetail.name;
    nameL.textAlignment = NSTextAlignmentCenter;
    nameL.font = [UIFont systemFontOfSize:20];
    [self addSubview:nameL];
    
    BaseLabel *recL = [[BaseLabel alloc] initWithFrame:CGRectMake(0, nameL.frame.size.height + nameL.frame.origin.y + 5, WIDTH, 30)];
    recL.textAlignment = NSTextAlignmentCenter;
    recL.font = [UIFont systemFontOfSize:13];
    NSString *str = [NSString stringWithFormat:@"推荐指数:%@", nearbyDetail.rating];
    recL.text = str;
    [self addSubview:recL];
    
    BaseLabel *recommended_reasonL = [[BaseLabel alloc] initWithFrame:CGRectMake(0, recL.frame.origin.y + recL.frame.size.height + 5, WIDTH, 30)];
    recommended_reasonL.text = nearbyDetail.recommended_reason;
    recommended_reasonL.textAlignment = NSTextAlignmentCenter;
    [self addSubview:recommended_reasonL];
    
//    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, recommended_reasonL.frame.origin.y + recommended_reasonL.frame.size.height + 2, WIDTH, 1)];
//    aView.backgroundColor = [UIColor lightGrayColor];
//    [self addSubview:aView];
    
    
    BaseLabel *messageL = [[BaseLabel alloc] initWithFrame:CGRectMake(3, recommended_reasonL.frame.origin.y + recommended_reasonL.frame.size.height + 10, WIDTH - 6, 35)];
    messageL.textAlignment = NSTextAlignmentCenter;
    messageL.text = @"-------详细信息介绍-------";
    messageL.font = [UIFont systemFontOfSize:22];
    [self addSubview:messageL];
    
    BaseLabel *lable1 = [[BaseLabel alloc] initWithFrame:CGRectMake(10, messageL.frame.origin.y + messageL.frame.size.height + 5, WIDTH, 20)];
    lable1.text = @"概况";
    [self addSubview:lable1];
    
    BaseLabel *descriptionPL = [[BaseLabel alloc] initWithFrame:CGRectMake(10, lable1.frame.origin.y + lable1.frame.size.height + 5, WIDTH - 10, [self heightCustom:nearbyDetail.descriptionP])];
    descriptionPL.text = nearbyDetail.descriptionP;
    descriptionPL.numberOfLines = 0;
    [self addSubview:descriptionPL];
    descriptionPL.font = [UIFont systemFontOfSize:15];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(5, descriptionPL.frame.origin.y + descriptionPL.frame.size.height + 2, WIDTH - 10, 1)];
    view1.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:view1];
    
    
    BaseLabel *lable2 = [[BaseLabel alloc] initWithFrame:CGRectMake(10, descriptionPL.frame.origin.y + descriptionPL.frame.size.height + 5, WIDTH, 20)];
    lable2.text = @"地址";
    [self addSubview:lable2];
    lable2.userInteractionEnabled = YES;
    
    BaseLabel *addressL = [[BaseLabel alloc] initWithFrame:CGRectMake(10, lable2.frame.origin.y + lable2.frame.size.height + 5, WIDTH - 10, [self heightCustom:nearbyDetail.address])];
    addressL.text = nearbyDetail.address;
    addressL.numberOfLines = 0;
    [self addSubview:addressL];
    addressL.font = [UIFont systemFontOfSize:15];
    addressL.userInteractionEnabled = YES;
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, descriptionPL.frame.origin.y + descriptionPL.frame.size.height + 5, WIDTH, 20 + [self heightCustom:nearbyDetail.address])];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:button];
    [button setTitle:@">" forState:UIControlStateNormal];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(5, addressL.frame.origin.y + addressL.frame.size.height + 2, WIDTH - 10, 1)];
    view2.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:view2];
 
    BaseLabel *lable3 = [[BaseLabel alloc] initWithFrame:CGRectMake(10, addressL.frame.origin.y + addressL.frame.size.height + 5, WIDTH, 20)];
    lable3.text = @"到达方式";
    [self addSubview:lable3];
    
    BaseLabel *arrival_typeL = [[BaseLabel alloc] initWithFrame:CGRectMake(10, lable3.frame.origin.y + lable3.frame.size.height + 5, WIDTH - 10, [self heightCustom:nearbyDetail.arrival_type])];
    arrival_typeL.text = nearbyDetail.arrival_type;
    arrival_typeL.numberOfLines = 0;
    [self addSubview:arrival_typeL];
    arrival_typeL.font = [UIFont systemFontOfSize:15];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(5, arrival_typeL.frame.origin.y + arrival_typeL.frame.size.height + 2, WIDTH - 10, 1)];
    view3.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:view3];
    
    
    
    BaseLabel *lable4 = [[BaseLabel alloc] initWithFrame:CGRectMake(10, arrival_typeL.frame.origin.y + arrival_typeL.frame.size.height + 5, WIDTH, 20)];
    lable4.text = @"开放时间";
    [self addSubview:lable4];
    
    BaseLabel *opening_timeL = [[BaseLabel alloc] initWithFrame:CGRectMake(10, lable4.frame.origin.y + lable4.frame.size.height + 5, WIDTH - 10, 20)];
    opening_timeL.text = nearbyDetail.opening_time;
    opening_timeL.numberOfLines = 0;
    [self addSubview:opening_timeL];
    opening_timeL.font = [UIFont systemFontOfSize:15];
    
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(5, opening_timeL.frame.origin.y + opening_timeL.frame.size.height + 2, WIDTH - 10, 1)];
    view4.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:view4];
    
    
    BaseLabel *lable5 = [[BaseLabel alloc] initWithFrame:CGRectMake(10, opening_timeL.frame.origin.y + opening_timeL.frame.size.height + 5, WIDTH, 20)];
    lable5.text = @"联系方式";
    [self addSubview:lable5];
    
    BaseLabel *telL = [[BaseLabel alloc] initWithFrame:CGRectMake(10, lable5.frame.origin.y + lable5.frame.size.height + 5, WIDTH - 10, 20)];
    telL.text = nearbyDetail.tel;
    telL.numberOfLines = 0;
    [self addSubview:telL];
    telL.font = [UIFont systemFontOfSize:15];
    
    
    self.contentSize = CGSizeMake(0, telL.frame.origin.y + 50);
}
-(CGFloat)heightCustom:(NSString *)content {
    //参数1.设置计算内容的宽度
    //参数2.设置计算高度
    //参数3.设置字体大小属性
    //参数4.系统备用参数,设置为nil
    
    //其中宽度一定与消失内容的label宽度一致,否则不准确
    CGSize size = CGSizeMake(WIDTH, 100000);
    
    //字体大小一定与显示内容的label字体大小一致,默认 17
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
    
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];

    
    return frame.size.height;
}

-(void)buttonAction:(UIButton *)button {
    
//    NSLog(@"button");
    
    [self.myDelegate location:self.nearbyDetail];
    
}

-(void)tapAction:(UITapGestureRecognizer *)tap {
    
    [self.myDelegate sendModle:self.nearbyDetail];

    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
